package com.ltwc.lttv.adapter

import android.graphics.Color
import android.view.ContextThemeWrapper
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.leanback.widget.ImageCardView
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.ltwc.lttv.R
import com.ltwc.lttv.tools.Utils
import com.ltwc.lttv.listener.ItemListener
import com.ltwc.lttv.models.TVListViewModel
import com.ltwc.lttv.models.TVViewModel
import com.ltwc.lttv.tools.ScaleAnimationTool


class CardAdapter(private val owner: LifecycleOwner, private var tvListViewModel: TVListViewModel) :
    RecyclerView.Adapter<CardAdapter.ViewHolder>() {

    private var listener: ItemListener? = null
    private var highlightedPosition = 0
    public fun setHighlightedPosition(position: Int) {
        highlightedPosition = position
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val cardView = object :
            ImageCardView(ContextThemeWrapper(parent.context, R.style.CustomImageCardTheme)) {}
        val layoutParams = parent.layoutParams as ViewGroup.MarginLayoutParams
        layoutParams.topMargin = Utils.dpToPx(5F)
        cardView.layoutParams = layoutParams
        return ViewHolder(cardView)
    }


    override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
        val item = tvListViewModel.getTVViewModel(position)
        viewHolder.view.tag = item
        val tvViewModel = item as TVViewModel
        val cardView = viewHolder.view as ImageCardView
        cardView.titleText = tvViewModel.title.value
        cardView.tag = "item.videoUrl"
        Glide.with(viewHolder.view.context).load(tvViewModel.logo.value).centerInside()
            .into(cardView.mainImageView)
        cardView.mainImageView.setBackgroundColor(Color.WHITE)
        cardView.setMainImageScaleType(ImageView.ScaleType.CENTER_INSIDE)
        viewHolder.itemView.setScaleX(0.9f);
        viewHolder.itemView.setScaleY(0.9f)
        cardView.setOnFocusChangeListener { view, hasFocus ->
            if (hasFocus) {
                view.animate().scaleX(1.0f).scaleY(1.0f).setDuration(200).start()
            } else {
                view.animate().scaleX(0.9f).scaleY(0.9f).setDuration(200).start()
            }
            listener?.onItemFocusChange(item, hasFocus)
        }
        if (highlightedPosition == position) {
            viewHolder.itemView.animate().scaleX(1.0f).scaleY(1.0f).setDuration(200).start()
        } else {
            viewHolder.itemView.animate().scaleX(0.9f).scaleY(0.9f).setDuration(200).start()
        }
        cardView.setOnClickListener { _ ->
            listener?.onItemClicked(item)
            setHighlightedPosition(position)
        }
        tvViewModel.program.observe(owner) { _ ->
            val program = tvViewModel.getProgramOne()
            if (program != null) {
                cardView.contentText = program.name
            } else {
                cardView.contentText = ""
            }
        }
    }

    override fun getItemCount() = tvListViewModel.size()

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val view = itemView
    }


    fun setItemListener(listener: ItemListener) {
        this.listener = listener
    }

    companion object {
        private const val TAG = "CardAdapter"
    }
}

